package test.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.junit.Test;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import com.benayn.ustyle.logger.Loggers;
import com.shareach.dao.mongo.impl.BizActivityShopDaoImpl;
import com.shareach.dao.mongo.impl.BizCardDaoImpl;
import com.shareach.dao.mongo.impl.BizTuanShopDaoImpl;
import com.shareach.domain.BizActivityShopDomain;
import com.shareach.domain.BizCardDomain;
import com.shareach.domain.BizTuanShopDomain;
import com.shareach.redis.RedisService;

@ContextConfiguration(locations = { "classpath:**/applicationContext.xml" })
public class RemoveMatched extends AbstractJUnit4SpringContextTests {
	
	/**
	 * 
	 */
	protected static final Log log = Loggers.from(RemoveMatched.class);
	
	@Resource 
	private RedisService redisService;
	@Resource
	private BizTuanShopDaoImpl tuanShopDao;
	@Resource
	private BizActivityShopDaoImpl actShopDao;
	@Resource
	private BizCardDaoImpl cardShopDao;
	
	

	@Test
	public void matchTest() {
		tuanShop();
		activityShop();
		cardShop();
		//删除对应的Collection数据，并将计数器置为空
		//商户
		//品牌
		//tag
		//分类
		//商圈
	}

	private void tuanShop(){
		//获取1000条
		long maxId = 0;
		while(true){
			Query query = new Query().limit(100);
			query.addCriteria(Criteria.where("_id").gt(maxId));
			query.sort().on("_id", Order.ASCENDING);
			query.fields().include("_id").include("shopId");
			List<BizTuanShopDomain> list = tuanShopDao.findList(query);
			for(BizTuanShopDomain item:list){
				maxId = item.getId();
				//将ShopId置为空
				//将优惠的ShopId放入未匹配队列
				log.debug(String.format("团购商户:%d,商户Id：%d", maxId, item.getShopId()));
			}
			if(list.isEmpty()) break;
		}
	}
	private void activityShop(){
		//获取1000条
		long maxId = 0;
		while(true){
			Query query = new Query().limit(100);
			query.addCriteria(Criteria.where("_id").gt(maxId));
			query.sort().on("_id", Order.ASCENDING);
			query.fields().include("_id").include("shopId");
			List<BizActivityShopDomain> list = actShopDao.findList(query);
			for(BizActivityShopDomain item:list){
				maxId = item.getId();
				//将ShopId置为空
				//将优惠的ShopId放入未匹配队列
				log.debug(String.format("活动商户:%d,商户Id：%d", maxId, item.getShopId()));
			}
			if(list.isEmpty()) break;
		}
	}
	private void cardShop(){
		//获取1000条
		long maxId = 0;
		while(true){
			Query query = new Query().limit(100);
			query.addCriteria(Criteria.where("_id").gt(maxId));
			query.sort().on("_id", Order.ASCENDING);
			query.fields().include("_id").include("shopId");
			List<BizCardDomain> list = cardShopDao.findList(query);
			for(BizCardDomain item:list){
				maxId = item.getId();
				//将ShopId置为空
				//将优惠的ShopId放入未匹配队列
				log.debug(String.format("卡惠商户:%d,商户Id：%d", maxId, item.getShopId()));
			}
			if(list.isEmpty()) break;
		}
	}
}
